//
//  UserData.m
//  openJira
//
//  Created by Vincent Demay on 04/05/10.
//  Copyright 2010 Goojet. All rights reserved.
//

#import "UserData.h"

static UserData* defaultData;

@implementation UserData
@synthesize login, password;

+ (UserData * )sharedData {
	if (!defaultData) {
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *documentsDirectory = [paths objectAtIndex:0];
		NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"userData.plist"];
		
		defaultData = [[NSKeyedUnarchiver unarchiveObjectWithFile:filePath] retain];
		
		if (!defaultData) {
			defaultData = [[UserData alloc] init];
		}
	}
	return defaultData;
}

+ (void) save {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"userData.plist"];
	[NSKeyedArchiver archiveRootObject:defaultData toFile:filePath];
}

+ (BOOL) hasUser {
	return [UserData sharedData].login != nil;
}

- (void) dealloc {
	[login release];
	[password release];
	[super dealloc];
}

#pragma mark NSCoding

- (void)encodeWithCoder:(NSCoder *)coder {
    //[super encodeWithCoder:coder];
    [coder encodeObject:login forKey:@"login"];
    [coder encodeObject:password forKey:@"password"];
}

- (id)initWithCoder:(NSCoder *)coder {
    //self = [super initWithCoder:coder];
    login = [[coder decodeObjectForKey:@"login"] retain];
    password  = [[coder decodeObjectForKey:@"password"] retain];
	return self;
}




@end
